import {getHttpHeaderFileName, blobSaveFile} from '@/libs/util'
let { Vue } = majesty
const saveExcelFile = (res, filename) => {
  const headName = getHttpHeaderFileName(res.headers)
  const blob = new Blob([res.data], {type: 'application/vnd.ms-excel'})
  blobSaveFile(blob, headName ? headName : filename)
}
export const excelExport = (url, params, method='post') => {
  console.log(params)
  let excelRes
  if (method === 'get') {
    excelRes = Vue.http.get(url, {responseType: 'blob'})
  }
  if (method === 'post') {
    excelRes = Vue.http.post(url, params, {responseType: 'blob'})
  }
  return excelRes.then(res => {
    saveExcelFile(res, `${params.name}.xls`)
  })
}
const savePdfFile = (res, filename) => {
  const headName = getHttpHeaderFileName(res.headers)
  const blob = new Blob([res.data], {type: 'application/pdf'})
  blobSaveFile(blob, headName ? headName : filename)
}
export const pdfExport = (url, params, method='post') => {
  console.log(params)
  let excelRes
  if (method === 'get') {
    excelRes = Vue.http.get(url, {responseType: 'blob'})
  }
  if (method === 'post') {
    excelRes = Vue.http.post(url, params, {responseType: 'blob'})
  }
  return excelRes.then(res => {
    savePdfFile(res, `${params.name}.pdf`)
  })
}
